package com.xh.bussiness.tiku.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.bussiness.tiku.bean.dto.QuestionChapterKnowledgeDTO;
import com.xh.bussiness.tiku.bean.request.QuestionChapterRequest;
import com.xh.bussiness.tiku.bean.request.QuestionKnowledgeRequest;
import com.xh.bussiness.tiku.dao.QuestionChapterKnowledgeMapper;
import com.xh.bussiness.tiku.model.*;
import com.xh.bussiness.tiku.service.*;
import com.xh.core.bean.XhPage;
import com.xh.core.service.impl.BaseServiceImpl;
import com.xh.core.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * <p>
 * 试题知识点表 服务实现类
 * </p>
 *
 * @author daiwei
 * @since 2019-01-14
 */
@Service
public class QuestionChapterKnowledgeKnowledgeServiceImpl extends BaseServiceImpl<QuestionChapterKnowledgeMapper, QuestionChapterKnowledge> implements IQuestionChapterKnowledgeService {

    @Autowired
    private IBookChapterService chapterService;
    @Autowired
    private IKnowledgeService knowledgeService;
    @Autowired
    private IQuestionKnowledgeService questionKnowledgeService;
    @Autowired
    private IChapterKnowledgeService chapterKnowledgeService;

    @Override
    public XhPage queryQuestionChapterPage(XhPage page, QuestionChapterRequest bean) {
        QueryWrapper qw=new QueryWrapper<QuestionChapterKnowledge>();
        //拼装条件
        qw=prepareParams(bean, qw);

        return (XhPage) this.dao.selectPage(page,qw);
    }

    @Override
    public List<QuestionChapterKnowledge> queryQuestionChapter(QuestionChapterRequest bean) {
        QueryWrapper qw=new QueryWrapper<QuestionChapterKnowledge>();
        //拼装条件
        qw=prepareParams(bean, qw);
        if(CollectionUtils.isEmpty(bean.getChapterIds())){
            qw.in("chapter_id",bean.getChapterIds());
        }

        return this.dao.selectList(qw);
    }


    /**
     * 准备条件
     * @param bean
     * @param qw
     */
    private QueryWrapper prepareParams(QuestionChapterRequest bean, QueryWrapper qw) {
        if(bean.getIds()!=null&&bean.getIds().length>0){
            qw.in("id",bean.getIds());
        }
        if (!StringUtils.isEmpty(bean.getQuestionId())) {
            qw.eq("question_id", bean.getQuestionId());
        }
        return qw;
    }

	@Override
	public void deleteByQuestionId(String questionId) {
		this.dao.deleteByQuestionId(questionId);
		
	}
	
	@Override
    public List<Integer> queryQuestionChapterCount(List<String> questionIds) {
        return this.dao.queryQuestionChapterCount(questionIds);
    }

    @Override
    public List<QuestionChapterKnowledgeDTO> queryByQuestionId(String questionId) {
        QueryWrapper<QuestionChapterKnowledge> qw = new QueryWrapper<>();
        qw.eq("question_id", questionId);

        List<QuestionChapterKnowledge> questionChapterKnowledges = this.dao.selectList(qw);
        if (CollectionUtils.isEmpty(questionChapterKnowledges)) {
            return new ArrayList<>();
        }
        List<QuestionChapterKnowledgeDTO> dtos = new ArrayList<>();
        for (QuestionChapterKnowledge item : questionChapterKnowledges) {
            QuestionChapterKnowledgeDTO dto = new QuestionChapterKnowledgeDTO();
            dto.setId(item.getId());

            String pathId = "";
            String pathName = "";
            if (!StringUtils.isEmpty(item.getChapterId())) {
                BookChapter bookChapter = chapterService.getById(item.getChapterId());
                if (bookChapter != null) {
                    dto.setChapterId(item.getChapterId());
                    dto.setChapterName(bookChapter.getName());

                    pathId = bookChapter.getPath().replace("#", "-");
                    pathName = bookChapter.getPathName().replace("#", "-");
                }
            }
            if (!StringUtils.isEmpty(item.getKnowledgeId())) {
                Knowledge knowledge = knowledgeService.getById(item.getKnowledgeId());
                if (knowledge != null) {
                    dto.setKnowledgeId(knowledge.getId());
                    dto.setKnowledgeName(knowledge.getName());

                    pathId += knowledge.getId();
                    pathName += knowledge.getName();
                }
            }
            dto.setPathId(pathId);
            dto.setPathName(pathName);

            dtos.add(dto);
        }
        return dtos;
    }

    @Override
    public void initQuestionChapterKnowledge() {
        QueryWrapper<QuestionKnowledge> questionKnowledgeQueryWrapper = new QueryWrapper<>();
        questionKnowledgeQueryWrapper.groupBy("question_id");

        List<QuestionKnowledge> allQuestion = questionKnowledgeService.queryByCondition(questionKnowledgeQueryWrapper);

        QuestionKnowledgeRequest questionKnowledgeRequest = new QuestionKnowledgeRequest();
        for (QuestionKnowledge questionKnowledge : allQuestion) {
            List<QuestionChapterKnowledge> datas = new ArrayList<>();
            questionKnowledgeRequest.setQuestionId(questionKnowledge.getQuestionId());
            List<QuestionKnowledge> questionKnowledges = questionKnowledgeService.queryQuestionKnowledge(questionKnowledgeRequest);
            if (!CollectionUtils.isEmpty(questionKnowledges)) {

                List<String> quesKnowledgeIds = questionKnowledges.stream().map(QuestionKnowledge::getKnowledgeId).collect(Collectors.toList());

                List<ChapterKnowledge> chapterKnowledges = chapterKnowledgeService.queryByKnowledgeIds(quesKnowledgeIds);

                if (!CollectionUtils.isEmpty(chapterKnowledges)) {
                    for (ChapterKnowledge chapterKnowledge : chapterKnowledges) {
                        QuestionChapterKnowledge item = new QuestionChapterKnowledge();
                        item.setId(UUID.randomUUID().toString().replace("-", ""));
                        item.setQuestionId(questionKnowledge.getQuestionId());
                        item.setChapterId(chapterKnowledge.getChapterId());
                        item.setKnowledgeId(chapterKnowledge.getKnowledgeId());
                        item.setCreateTime(new Date());
                        datas.add(item);
                    }
                }
            }

            if (!CollectionUtils.isEmpty(datas)) {
                this.dao.batchInsert(datas);
            }
        }

    }
}
